If you have a pattern of related fields that occurs in several places in your model, it may be convenient to pull these out into a value object. An example is an Address value object: a Customer might have home and work addresses, and a Warehouse a street address, all comprising the same set of fields. It may even be useful to use a value object just to break up your object model into smaller and easier-to-understand chunks.

To create a value object, open the Toolbox and drag the Value Object icon onto the design surface. You can name and add properties to the value object just as for entities.

To use a value object, open the Toolbox, select the Value Object Property tool, and drag the mouse from an entity to the value object. For example, to declare a home address for a customer, drag from the Customer entity to the Address value object.

Value Object Mappings

Value objects are mapped as NHibernate components. By default, a value object property maps to a column of the same name. To override this on a particular use of the value object, select the connector from the entity to the value object, go to the Properties window, and edit the “Column Name for…” setting. This allows different mappings in different uses: for example, the City member of the Address value object could be mapped to HomeCity for the Customer Home address and WorkCity for the Customer Work address.

Sometimes a value object property should not be mapped in a specific instance. For example, the database might include country columns for the Customer Home and Work addresses, but not for the Warehouse address. Rather than having two different address types, you can specify that the Address.Country property is not mapped in the Warehouse case. To do this, select the connector and change the “Is Mapped for…” setting to False.